﻿#include "ordermanagebus.h"
#include "ui_ordermanagebus.h"
#include <QMessageBox>

OrderManageBus::OrderManageBus(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::OrderManageBus)
{
    ui->setupUi(this);
    m_modifyState = nullptr;

    m_modelAll = new OrderManageModel;
    m_modelNo = new OrderManageModel;
    ui->tabV_all->setModel(m_modelAll);
    ui->tabV_no->setModel(m_modelNo);
    ui->tabV_all->setSelectionBehavior(QAbstractItemView::SelectRows); //整行选中的方式
    ui->tabV_all->horizontalHeader()->setStretchLastSection(true);
    ui->tabV_all->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
//    ui->tabV_all->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tabV_no->setSelectionBehavior(QAbstractItemView::SelectRows); //整行选中的方式
    ui->tabV_no->horizontalHeader()->setStretchLastSection(true);
    ui->tabV_no->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
//    ui->tabV_no->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    QDate date = QDate::currentDate();
    m_selectAll = m_selectNo = D_None;
    ui->de_endAll->setDate(date);
    ui->de_endNo->setDate(date);
    ui->de_startAll->setDate(date);
    ui->de_startNo->setDate(date);
    ui->de_endAll->setDisplayFormat("yyyy.M.d");
    ui->de_endNo->setDisplayFormat("yyyy.M.d");
    ui->de_startAll->setDisplayFormat("yyyy.M.d");
    ui->de_startNo->setDisplayFormat("yyyy.M.d");
    ui->de_endAll->setCalendarPopup(true);
    ui->de_endNo->setCalendarPopup(true);
    ui->de_startAll->setCalendarPopup(true);
    ui->de_startNo->setCalendarPopup(true);
}

OrderManageBus::~OrderManageBus()
{
    delete ui;
}

void OrderManageBus::setOptionalHotel()
{
    ui->cb_selectHotel->clear();
    m_hotelIdNameMap.clear();
    for(HotelInfoList::iterator it = GlobalVars::g_hotelManageList->begin();
        it != GlobalVars::g_hotelManageList->end(); it++)
    {
        m_hotelIdNameMap.insert(it->getHotel(),it->getName());
    }
    ui->cb_selectHotel->addItems(m_hotelIdNameMap.values());
}

QString OrderManageBus::getCurrentSelectedHotelID()
{
    return m_hotelIdNameMap.key(ui->cb_selectHotel->currentText());
}

void OrderManageBus::slotModifyOrderStateResult(bool result,const QString& msg)
{
    if (result)
    {
        m_modifyState->close();
        on_pb_update_clicked();
    }
    else
    {
        m_modifyState->errorShow(msg);
    }
}
void OrderManageBus::slotUpdateOrderManageShow()
{
    m_modelAll->refrushModel(m_selectAll);
    m_modelNo->refrushModel(m_selectNo);
}

void OrderManageBus::judgeNoSelectState()
{
    m_modelNo->refrushModel(m_selectNo);
    switch (m_selectNo)
    {
    case D_None: break;
    case D_TodayNo:
        ui->pb_todayNo->setStyleSheet("background-color: #13d1be;");
        ui->pb_weekNo->setStyleSheet("background-color: transparent;");
        ui->pb_monthNo->setStyleSheet("background-color: transparent;");
        ui->pb_yesterdayNo->setStyleSheet("background-color: transparent;");
        ui->de_startNo->setStyleSheet("background-color: none;");
        ui->de_endNo->setStyleSheet("background-color: none;");
        break;
    case D_YesterdayNo:
        ui->pb_todayNo->setStyleSheet("background-color: transparent;");
        ui->pb_weekNo->setStyleSheet("background-color: transparent;");
        ui->pb_monthNo->setStyleSheet("background-color: transparent;");
        ui->pb_yesterdayNo->setStyleSheet("background-color: #13d1be;");
        ui->de_startNo->setStyleSheet("background-color: none;");
        ui->de_endNo->setStyleSheet("background-color: none;");
        break;
    case D_WeekNo:
        ui->pb_todayNo->setStyleSheet("background-color: transparent;");
        ui->pb_weekNo->setStyleSheet("background-color: #13d1be;");
        ui->pb_monthNo->setStyleSheet("background-color: transparent;");
        ui->pb_yesterdayNo->setStyleSheet("background-color: transparent;");
        ui->de_startNo->setStyleSheet("background-color: none;");
        ui->de_endNo->setStyleSheet("background-color: none;");
        break;
    case D_MonthNo:
        ui->pb_todayNo->setStyleSheet("background-color: transparent;");
        ui->pb_weekNo->setStyleSheet("background-color: transparent;");
        ui->pb_monthNo->setStyleSheet("background-color: #13d1be;");
        ui->pb_yesterdayNo->setStyleSheet("background-color: transparent;");
        ui->de_startNo->setStyleSheet("background-color: none;");
        ui->de_endNo->setStyleSheet("background-color: none;");
        break;
    case D_CustomizeNo:
        ui->pb_todayNo->setStyleSheet("background-color: transparent;");
        ui->pb_weekNo->setStyleSheet("background-color: transparent;");
        ui->pb_monthNo->setStyleSheet("background-color: transparent;");
        ui->pb_yesterdayNo->setStyleSheet("background-color: transparent;");
        ui->de_startNo->setStyleSheet("background-color: #13d1be;");
        ui->de_endNo->setStyleSheet("background-color: #13d1be;");
        break;
    default: break;
    }
}
void OrderManageBus::judgeALLSelectState()
{
    m_modelAll->refrushModel(m_selectAll);
    switch (m_selectAll)
    {
    case D_None: break;
    case D_TodayAll:
        ui->pb_todayAll->setStyleSheet("background-color: #13d1be;");
        ui->pb_weekAll->setStyleSheet("background-color: transparent;");
        ui->pb_monthAll->setStyleSheet("background-color: transparent;");
        ui->pb_yesterdayAll->setStyleSheet("background-color: transparent;");
        ui->de_startAll->setStyleSheet("background-color: none;");
        ui->de_endAll->setStyleSheet("background-color: none;");
        break;
    case D_YesterdayAll:
        ui->pb_todayAll->setStyleSheet("background-color: transparent;");
        ui->pb_weekAll->setStyleSheet("background-color: transparent;");
        ui->pb_monthAll->setStyleSheet("background-color: transparent;");
        ui->pb_yesterdayAll->setStyleSheet("background-color: #13d1be;");
        ui->de_startAll->setStyleSheet("background-color: none;");
        ui->de_endAll->setStyleSheet("background-color: none;");
        break;
    case D_WeekAll:
        ui->pb_todayAll->setStyleSheet("background-color: transparent;");
        ui->pb_weekAll->setStyleSheet("background-color: #13d1be;");
        ui->pb_monthAll->setStyleSheet("background-color: transparent;");
        ui->pb_yesterdayAll->setStyleSheet("background-color: transparent;");
        ui->de_startAll->setStyleSheet("background-color: none;");
        ui->de_endAll->setStyleSheet("background-color: none;");
        break;
    case D_MonthAll:
        ui->pb_todayAll->setStyleSheet("background-color: transparent;");
        ui->pb_weekAll->setStyleSheet("background-color: transparent;");
        ui->pb_monthAll->setStyleSheet("background-color: #13d1be;");
        ui->pb_yesterdayAll->setStyleSheet("background-color: transparent;");
        ui->de_startAll->setStyleSheet("background-color: none;");
        ui->de_endAll->setStyleSheet("background-color: none;");
        break;
    case D_CustomizeAll:
        ui->pb_todayAll->setStyleSheet("background-color: transparent;");
        ui->pb_weekAll->setStyleSheet("background-color: transparent;");
        ui->pb_monthAll->setStyleSheet("background-color: transparent;");
        ui->pb_yesterdayAll->setStyleSheet("background-color: transparent;");
        ui->de_startAll->setStyleSheet("background-color: #13d1be;");
        ui->de_endAll->setStyleSheet("background-color: #13d1be;");
        break;
    default: break;
    }
}
void OrderManageBus::on_pb_todayAll_clicked()
{
    m_selectAll = D_TodayAll;
    judgeALLSelectState();
}
void OrderManageBus::on_pb_yesterdayAll_clicked()
{
    m_selectAll = D_YesterdayAll;
    judgeALLSelectState();
}
void OrderManageBus::on_pb_weekAll_clicked()
{
    m_selectAll = D_WeekAll;
    judgeALLSelectState();
}
void OrderManageBus::on_pb_monthAll_clicked()
{
    m_selectAll = D_MonthAll;
    judgeALLSelectState();
}
void OrderManageBus::on_pb_todayNo_clicked()
{
    m_selectNo = D_TodayNo;
    judgeNoSelectState();
}
void OrderManageBus::on_pb_yesterdayNo_clicked()
{
    m_selectNo = D_YesterdayNo;
    judgeNoSelectState();
}
void OrderManageBus::on_pb_weekNo_clicked()
{
    m_selectNo = D_WeekNo;
    judgeNoSelectState();
}
void OrderManageBus::on_pb_monthNo_clicked()
{
    m_selectNo = D_MonthNo;
    judgeNoSelectState();
}
void OrderManageBus::on_de_startAll_dateChanged(const QDate &date)
{
    m_modelAll->setCustomizeDay(date,ui->de_endAll->date());
    m_selectAll = D_CustomizeAll;
    judgeALLSelectState();
}
void OrderManageBus::on_de_endAll_dateChanged(const QDate &date)
{
    m_modelAll->setCustomizeDay(ui->de_startAll->date(),date);
    m_selectAll = D_CustomizeAll;
    judgeALLSelectState();
}
void OrderManageBus::on_de_startNo_dateChanged(const QDate &date)
{
    m_modelNo->setCustomizeDay(date,ui->de_endNo->date());
    m_selectNo = D_CustomizeNo;
    judgeNoSelectState();
}
void OrderManageBus::on_de_endNo_dateChanged(const QDate &date)
{
    m_modelNo->setCustomizeDay(ui->de_startNo->date(),date);
    m_selectNo = D_CustomizeNo;
    judgeNoSelectState();
}

void OrderManageBus::on_tabV_all_doubleClicked(const QModelIndex &index)
{
    QString checkInName = m_modelAll->getCheckInName(index.row());
    QString message = "确定修改 "+ checkInName + " 的订单状态";
    QMessageBox::StandardButton result = QMessageBox::warning(this, "警告",
                         message, QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);

    if (result == QMessageBox::Yes)
    {
        QStringList strList;
        strList << ui->cb_selectHotel->currentText();
        strList << checkInName;
        strList << m_modelAll->getHouseType(index.row());
        strList << m_modelAll->getOrderState(index.row());
        strList << m_modelAll->getReserveID(index.row());

        if (m_modifyState != nullptr)
        {
            delete m_modifyState;
        }
        m_modifyState = new ModifyOrderState();
        m_modifyState->setInfoShow(strList);
        m_modifyState->setWindowTitle("修改订单状态");
        m_modifyState->setWindowModality(Qt::ApplicationModal);
        m_modifyState->setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint); // 禁止最大化按钮
        m_modifyState->setFixedSize(m_modifyState->width(),m_modifyState->height());   // 禁止拖动窗口大小
        m_modifyState->show();

        connect(m_modifyState,SIGNAL(signalModifyOrderState(const QString&, const QString&)),
                     this,SIGNAL(signalModifyOrderState(const QString&, const QString&)));
    }
}

void OrderManageBus::on_pb_update_clicked()
{
    emit signalGainOrderManageList(m_hotelIdNameMap.key(ui->cb_selectHotel->currentText()));
}

void OrderManageBus::on_cb_selectHotel_currentIndexChanged(const QString &arg1)
{
    if (!arg1.isEmpty())
    {
        emit signalGainOrderManageList(m_hotelIdNameMap.key(arg1));
    }
}
